﻿using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;

namespace ReaWTM.LayuiHelper
{

	[HtmlTargetElement("wt:localdata", Attributes = "name", TagStructure = TagStructure.WithoutEndTag)]
	public class LocalDataTagHelper : BaseTag
	{
		public bool IsOnce { get; set; }
		public string Name { get; set; }
		public string Key { get; set; }
		public object Value { get; set; }
		public override void Process(TagHelperContext context, TagHelperOutput output)
		{
			output.TagName = "script";
			output.TagMode = TagMode.StartTagAndEndTag;

			string mode = IsOnce ? "layui.sessionData" : "layui.data";

			StringBuilder sb = new StringBuilder(mode);
			sb.Append($"('{Name}',");
			sb.Append("{");
			sb.Append($"key:'{Key}',value:{JsonSerializer.Serialize(Value, JsonOptions)}");
			sb.Append("});");
			output.Content.SetHtmlContent(sb.ToString());


		}
	}
}
